package oop.core.skill;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

public class SkillLibraryLoader {
	public static void init(){
		File fileDir = new File("skillzDescriptions/");
		File[] files = fileDir.listFiles();
		FileInputStream f = null;
		
		for(int k=0; k<files.length; k++)
		{
			Skill newSkill = null;
			try
			{
				String fileName = files[k].getName();
				if(fileName.equals(".svn")){
					continue;
				}
				f = new FileInputStream(files[k]);
				Scanner scanner = new Scanner(f);
				
				String name = scanner.nextLine();
				int skillLevel = Integer.parseInt(scanner.nextLine());
				
				String type = scanner.nextLine();
				
				if(type.equals("InteractiveSkill")){
					String subtype = scanner.nextLine();
					scanner.nextLine();
					
					if(subtype.equals("Sleep")){
						int delayDuration = Integer.parseInt(scanner.nextLine());
						newSkill = new Sleep(delayDuration,name,skillLevel);
					}
					else if(subtype.equals("PickPocket")){
						newSkill = new PickPocket(name,skillLevel);
					}
					else if(subtype.equals("Enchantment")){
						newSkill = new Enchantment(name,skillLevel);
					}
					else
						throw new IOException();
				}
				else if(type.equals("PassiveSkill")){
					newSkill = new PassiveSkill(name,skillLevel);
				}
				else if(type.equals("ActiveSkill")){
					String subtype = scanner.nextLine();
					scanner.nextLine();
					
					String[] properties = scanner.nextLine().split(",");
					
					long lifeTime = Long.parseLong(properties[0]);
					long hitCount = Long.parseLong(properties[1]);
					float effect = Long.parseLong(properties[2]);
					String image = properties[3];
					long propogationDelay = Long.parseLong(properties[4]);
					int range = Integer.parseInt(properties[5]);
					
					String[] pFactors = scanner.nextLine().split(",");
					String[] weaponTypes = scanner.nextLine().split(",");
					scanner.nextLine();
					
					List<String> p = Arrays.asList(pFactors);
					List<String> w = Arrays.asList(weaponTypes);
					
					if(subtype.equals("Cone")){
						boolean radial = scanner.nextLine() == "true";
						newSkill = new Cone(radial,name,skillLevel,lifeTime,hitCount,effect,image,propogationDelay,range,p,w);
					}
					else if(subtype.equals("Radial")){
						newSkill = new Radial(name,skillLevel,lifeTime,hitCount,effect,image,propogationDelay,range,p,w);
					}
					else
						throw new IOException();
				}
				else
					throw new IOException();
			}
			catch (IOException ioe)
			{
				ioe.printStackTrace();
				continue;
			}
			try{
				f.close();
			}
			catch(IOException ioe){
				continue;
			}
			SkillLibrary.addToLibrary(newSkill);
		}
	}
}
